﻿/*********************************************************
 * 开发人员：小兵
 * 创建时间：2012-10-18 11:13:10
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/
namespace Mozlite.WebUI.Ribbon
{
    using System;
    using System.Web;
    using System.Data;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Xml;

    /// <summary>
    /// Ribbon管理类。
    /// </summary>
    public static class Ribbons
    {
        /// <summary>
        /// 获取Ribbon实例。
        /// </summary>
        /// <param name="path">配置文件物理路径。</param>
        /// <returns>返回Ribbon实例。</returns>
        public static Ribbon GetRibbon(string path = null) {
            //return Caches.Get<Ribbon>(cacheKey, () =>
            //{
                if(path == null)
                    path = Cores.GetPath("~/App_Utilities/Ribbon/ribbon.config");
                if(!File.Exists(path))
                    throw new Exception("The ribbon.config file is not existed!");
                XmlDocument doc = new XmlDocument();
                doc.Load(path);
                var root = doc.SelectSingleNode("ribbon");
                if(root == null)
                    throw new Exception("The ribbon document node is not found in ribbon.config!");
                var ribbon = new Ribbon();
                foreach(XmlNode node in root)
                {
                    if(node.NodeType == XmlNodeType.Comment)
                        continue;
                    switch(node.Name)
                    {
                        case "orb":
                            ribbon.Orb = new Orb(node);
                            break;
                        case "menu":
                            ribbon.Menu.Add(new Menu(node));
                            break;
                    }
                }
                //Caches.Insert(cacheKey, ribbon, new System.Web.Caching.CacheDependency(path));
                return ribbon;
            //});
        }

        private const string cacheKey = "RibbonsFromXmlFile";
    }
}
